package com.example.javaairportflightanalysis.service.impl;

import com.example.javaairportflightanalysis.dao.AirLineMapper;
import com.example.javaairportflightanalysis.dao.BaseApueMapper;
import com.example.javaairportflightanalysis.dao.DfdlMapper;

import com.example.javaairportflightanalysis.domin.dto.BaseApueCnnm;
import com.example.javaairportflightanalysis.domin.dto.DfdlDto;
import com.example.javaairportflightanalysis.domin.entity.AirLine;
import com.example.javaairportflightanalysis.domin.merge.DfdlDtoMerge;

import com.example.javaairportflightanalysis.domin.merge.DfdlStatusDto;
import com.example.javaairportflightanalysis.domin.merge.TransferMerge;
import com.example.javaairportflightanalysis.domin.merge.TransferStatusDto;
import com.example.javaairportflightanalysis.service.DfdlService;
import com.example.javaairportflightanalysis.utils.GroupAndPageUtil;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author shen
 */
@Service
@Slf4j
public class DfdlServiceImpl implements DfdlService {
    @Resource
    DfdlMapper dfdlMapper;
    @Resource
    BaseApueMapper baseApueMapper;
    @Resource
    AirLineMapper airLineMapper;
    /**
     * 根据airlArptId分组
     * 这个是国内/国际到港
     */
    @Override
    public PageInfo getAApno2(Integer pageNum, Integer pageSize) throws ParseException {
        ArrayList<DfdlStatusDto> transferMerges = dfdlMapper.getAapno2();
        //过滤出transferMerges的status为0的数据，0为审核成功，1为正在审核，2为审核驳回
        Predicate<DfdlStatusDto> filter = t -> t.getStatus() == 0;
        List<DfdlStatusDto> filteredList = filterList(transferMerges, filter);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(filteredList, pageNum, pageSize);
        return pageInfo;
    }

    private List<DfdlDtoMerge> processA2401Dtos2(List<List<DfdlDto>> groupList, List<AirLine> airLines, List<BaseApueCnnm> baseApueCnnm) throws ParseException {
        List<DfdlDtoMerge> mergeList = new ArrayList<>();
        for (List<DfdlDto> A2401Dtos : groupList) {
            StringBuilder sb = new StringBuilder();
            DfdlDtoMerge dfdlDtoMerge = new DfdlDtoMerge();
            String tempApcd1 = null;
            String tempApcd2 = null;
            String date1;
            // 用来存储Apno=2的数据
            if (A2401Dtos.size() == 2) {
                for (DfdlDto A2401Dto : A2401Dtos) {
                    dfdlDtoMerge.setDfltFlid(A2401Dto.getDfltFlid());
                    //DfltFlno需要加上awcd
                    dfdlDtoMerge.setDfltFlno(A2401Dto.getDfltAwcd() + A2401Dto.getDfltFlno());
                    //DfltAwcd需要使用数据库替换
                    boolean flag = false;
                    for (AirLine airLine : airLines) {
                        if (Objects.equals(airLine.getCode(), A2401Dto.getDfltAwcd())) {
                            dfdlDtoMerge.setDfltAwcd(airLine.getName());
                            flag = true;
                        }
                    }
                    if (!flag) {
                        dfdlDtoMerge.setDfltAwcd(A2401Dto.getDfltAwcd());
                    }
                    String date = GroupAndPageUtil.getDate(A2401Dto.getDtime());
                    dfdlDtoMerge.setDtime(date);
                    //当apno=1时，赋值Fplt
                    if (Objects.equals(A2401Dto.getApno(), "1")) {
                        date1 = GroupAndPageUtil.getDate(A2401Dto.getFptt());
                        dfdlDtoMerge.setFptt(date1);
                        //当apno=1时，赋值Apcd
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), A2401Dto.getApcd())) {
                                tempApcd1 = apueCnnm.getCNNM();
                                found= true;
                                break;
                            }
                        }
                        if (!found) {
                            tempApcd1 = A2401Dto.getApcd();
                        }
                    }
                    //当apno=2时，赋值Fplt
                    if (Objects.equals(A2401Dto.getApno(), "2")) {
                        date1 = GroupAndPageUtil.getDate(A2401Dto.getFplt());
                        dfdlDtoMerge.setFplt(date1);
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), A2401Dto.getApcd())) {
                                tempApcd2 = (apueCnnm.getCNNM());
                            }
                            if (Objects.equals(A2401Dto.getApcd(), "TSN")) {
                                tempApcd2 = "天津";
                            }
                        }
                    }
                }
                sb.append(tempApcd1).append("-").append(tempApcd2);
                dfdlDtoMerge.setApcd(sb.toString());
                mergeList.add(dfdlDtoMerge);
            }
        }
        return mergeList;
    }

    @Override
    public PageInfo getAApno3(Integer pageNum, Integer pageSize) {
        ArrayList<TransferStatusDto> transferMerges = dfdlMapper.getAapno3();
        //过滤出transferMerges的status为0的数据，0为审核成功，1为正在审核，2为审核失败
        Predicate<TransferStatusDto> filter = t -> t.getStatus() == 0;
        List<TransferStatusDto> filteredList = filterList(transferMerges, filter);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(filteredList, pageNum, pageSize);
        return pageInfo;
    }

    private List<TransferMerge> processA2401Dtos3(List<List<DfdlDto>> groupList, List<AirLine> airLines, List<BaseApueCnnm> baseApueCnnm) throws ParseException {
        List<TransferMerge> mergeList = new ArrayList<>();
        for (List<DfdlDto> a2403Dtos : groupList) {
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            StringBuilder fplt = new StringBuilder();
            StringBuilder fptt = new StringBuilder();
            TransferMerge dfdlDtoMerge = new TransferMerge();
            String tempApcd1 = null;
            String tempApcd2 = null;
            String tempApcd3 = null;
            String date1 = null;
            String date2 = null;
            String date3 = null;
            String date4 = null;
            // 用来存储Apno=2的数据
            if (a2403Dtos.size() == 3) {
                for (DfdlDto a2403Dto : a2403Dtos) {
                    dfdlDtoMerge.setDfltFlid(a2403Dto.getDfltFlid());
                    dfdlDtoMerge.setDfltFlno(a2403Dto.getDfltAwcd() + a2403Dto.getDfltFlno());
                    //DfltAwcd需要使用数据库替换
                    boolean flag = false;
                    for (AirLine airLine : airLines) {
                        if (Objects.equals(airLine.getCode(), a2403Dto.getDfltAwcd())) {
                            dfdlDtoMerge.setDfltAwcd(airLine.getName());
                            flag = true;
                        }
                    }
                    if (!flag) {
                        dfdlDtoMerge.setDfltAwcd(a2403Dto.getDfltAwcd());
                    }
                    String date = GroupAndPageUtil.getDate(a2403Dto.getDtime());
                    dfdlDtoMerge.setDtime(date);
                    //当apno=1时，赋值Fplt
                    if (Objects.equals(a2403Dto.getApno(), "1")) {
                        date1 = GroupAndPageUtil.getDate(a2403Dto.getFptt());
                        //当apno=1时，赋值Apcd
                        // 用来标记是否找到了相等的值
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), a2403Dto.getApcd())) {
                                tempApcd1 = (apueCnnm.getCNNM());
                                // 如果找到了相等的值，就把found设为true
                                found = true;
                                break;
                            }
                        }
                        // 如果循环结束后，found还是false，说明没有找到相等的值
                        if (!found) {
                            tempApcd1 = a2403Dto.getApcd();
                        }
                    }
                    //当apno=2时，赋值Fplt
                    if (Objects.equals(a2403Dto.getApno(), "2")) {
                        date2 = GroupAndPageUtil.getDate(a2403Dto.getFptt());
                        date3 = GroupAndPageUtil.getDate(a2403Dto.getFplt());
                        // 用来标记是否找到了相等的值
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), a2403Dto.getApcd())) {
                                tempApcd2 = (apueCnnm.getCNNM());
                                // 如果找到了相等的值，就把found设为true
                                found = true;
                                break;
                            }
                        }
                        // 如果循环结束后，found还是false，说明没有找到相等的值
                        if (!found) {
                            tempApcd2 = a2403Dto.getApcd();
                        }
                    }
                    //当apno=3时，赋值Fplt
                    if (Objects.equals(a2403Dto.getApno(), "3")) {
                        date4 = GroupAndPageUtil.getDate(a2403Dto.getFplt());
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), a2403Dto.getApcd())) {
                                tempApcd3 = (apueCnnm.getCNNM());
                            }
                            if (Objects.equals(a2403Dto.getApcd(), "TSN")) {
                                tempApcd3 = "天津";
                            }
                        }
                    }
                }
                sb.append(tempApcd1).append("-").append(tempApcd2);
                dfdlDtoMerge.setApcd(sb.toString());
                sb1.append(tempApcd2).append("-").append(tempApcd3);
                dfdlDtoMerge.setTransitApcd(sb1.toString());
                fptt.append(date1).append(" 到 ").append(date3);
                dfdlDtoMerge.setFptt(fptt.toString());
                fplt.append(date2).append(" 到 ").append(date4);
                dfdlDtoMerge.setFplt(fplt.toString());
                mergeList.add(dfdlDtoMerge);
            }
        }
        return mergeList;
    }
    /**
     * 这个是国际/国内离港
     */
    @Override
    public PageInfo getDApno2(Integer pageNum, Integer pageSize) {
        ArrayList<DfdlStatusDto> mergeList = dfdlMapper.getDapno2();
        Predicate<DfdlStatusDto> filter = t -> t.getStatus() == 0;
        List<DfdlStatusDto> filteredList = filterList(mergeList, filter);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(filteredList, pageNum, pageSize);
        return pageInfo;
    }
    /**
     * 用来处理Dapno为2的数据
     */
    private List<DfdlDtoMerge> processD2401Dtos2(List<List<DfdlDto>> groupList, List<AirLine> airLines, List<BaseApueCnnm> baseApueCnnm) throws ParseException {
        List<DfdlDtoMerge> mergeList = new ArrayList<>();
        for (List<DfdlDto> d2401Dtos : groupList) {
            StringBuilder sb = new StringBuilder();
            DfdlDtoMerge dfdlDtoMerge = new DfdlDtoMerge();
            String tempApcd1 = null;
            String tempApcd2 = null;
            String date1;
            // 用来存储Apno=2的数据
            if (d2401Dtos.size() == 2) {
                for (DfdlDto d2401Dto : d2401Dtos) {
                    dfdlDtoMerge.setDfltFlid(d2401Dto.getDfltFlid());
                    //DfltFlno需要加上awcd
                    dfdlDtoMerge.setDfltFlno(d2401Dto.getDfltAwcd() + d2401Dto.getDfltFlno());
                    //DfltAwcd需要使用数据库替换
                    boolean airfound = false;
                    for (AirLine airLine : airLines) {
                        if (Objects.equals(airLine.getCode(), d2401Dto.getDfltAwcd())) {
                            dfdlDtoMerge.setDfltAwcd(airLine.getName());
                            airfound = true;
                        }
                    }
                    if (!airfound) {
                        dfdlDtoMerge.setDfltAwcd(d2401Dto.getDfltAwcd());
                    }
                    String date = GroupAndPageUtil.getDate(d2401Dto.getDtime());
                    dfdlDtoMerge.setDtime(date);
                    //当apno=1时，赋值Fplt
                    if (Objects.equals(d2401Dto.getApno(), "1")) {
                        date1 = GroupAndPageUtil.getDate(d2401Dto.getFptt());
                        dfdlDtoMerge.setFptt(date1);
                        if (Objects.equals(d2401Dto.getApcd(), "TSN")) {
                            tempApcd1 = "天津";
                        }
                    }
                    //当apno=2时，赋值Fplt
                    if (Objects.equals(d2401Dto.getApno(), "2")) {
                        date1 = GroupAndPageUtil.getDate(d2401Dto.getFplt());
                        dfdlDtoMerge.setFplt(date1);
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), d2401Dto.getApcd())) {
                                tempApcd2 = (apueCnnm.getCNNM());
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            tempApcd2 = d2401Dto.getApcd();
                        }
                    }
                }
                sb.append(tempApcd1).append("-").append(tempApcd2);
                dfdlDtoMerge.setApcd(sb.toString());
                mergeList.add(dfdlDtoMerge);
            }
        }
        return mergeList;
    }

    @Override
    public PageInfo getDApno3(Integer pageNum, Integer pageSize) {
        ArrayList<TransferStatusDto> dfdlDtoMerges = dfdlMapper.getDapno3();
        Predicate<TransferStatusDto> filter = t -> t.getStatus() == 0;
        List<TransferStatusDto> filteredList = filterList(dfdlDtoMerges, filter);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(filteredList, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 用来处理Dapno为3的数据
     */
    private List<TransferMerge> processD2401Dtos3(List<List<DfdlDto>> groupList, List<AirLine> airLines, List<BaseApueCnnm> baseApueCnnm) throws ParseException {
        List<TransferMerge> mergeList = new ArrayList<>();
        for (List<DfdlDto> d2401Dtos : groupList) {
            TransferMerge dfdlDtoMerge = new TransferMerge();
            StringBuilder fptt = new StringBuilder();
            StringBuilder fplt = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            String tempApcd1 = null;
            String tempApcd2 = null;
            String tempApcd3 = null;
            String date1 = null;
            String date2 = null;
            String date3 = null;
            String date4 = null;
            if (d2401Dtos.size() == 3) {
                for (DfdlDto d2401Dto : d2401Dtos) {
                    dfdlDtoMerge.setDfltFlid(d2401Dto.getDfltFlid());
                    dfdlDtoMerge.setDfltFlno(d2401Dto.getDfltAwcd() + d2401Dto.getDfltFlno());
                    //使用数据库替换
                    boolean airfound = false;
                    for (AirLine airLine : airLines) {
                        if (Objects.equals(airLine.getCode(), d2401Dto.getDfltAwcd())) {
                            dfdlDtoMerge.setDfltAwcd(airLine.getName());
                            airfound = true;
                            break;
                        }
                    }
                    if (!airfound) {
                        dfdlDtoMerge.setDfltAwcd(d2401Dto.getDfltAwcd());
                    }
                    String date = GroupAndPageUtil.getDate(d2401Dto.getDtime());
                    dfdlDtoMerge.setDtime(date);

                    //当apno=1时，赋值Fplt
                    if (Objects.equals(d2401Dto.getApno(), "1")) {
                        date1 = GroupAndPageUtil.getDate(d2401Dto.getFptt());
                        if (Objects.equals(d2401Dto.getApcd(), "TSN")) {
                            tempApcd1 = "天津";
                        }
                    }
                    //当apno=2时，赋值Fplt
                    if (Objects.equals(d2401Dto.getApno(), "2")) {
                        date2 = GroupAndPageUtil.getDate(d2401Dto.getFptt());
                        date3 = GroupAndPageUtil.getDate(d2401Dto.getFplt());
                        // 用来标记是否找到了相等的值
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), d2401Dto.getApcd())) {
                                tempApcd2 = (apueCnnm.getCNNM());
                                // 如果找到了相等的值，就把found设为true
                                found = true;
                                break;
                            }
                        }
//                         如果循环结束后，found还是false，说明没有找到相等的值
                        if (!found) {
                            tempApcd2 = d2401Dto.getApcd();
                        }
                    }
                    //当apno=3时，赋值Fplt
                    if (Objects.equals(d2401Dto.getApno(), "3")) {
                        date4 = GroupAndPageUtil.getDate(d2401Dto.getFplt());
                        boolean found = true;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), d2401Dto.getApcd())) {
                                tempApcd3 = (apueCnnm.getCNNM());
                                // 如果找到了相等的值，就把found设为true
                                found = false;
                                break;
                            }
                        }
                        if (found) {
                            tempApcd3 = d2401Dto.getApcd();
                        }
                    }
                }
                sb.append(tempApcd1).append("-").append(tempApcd2);
                dfdlDtoMerge.setApcd(sb.toString());
                sb1.append(tempApcd2).append("-").append(tempApcd3);
                dfdlDtoMerge.setTransitApcd(sb1.toString());
                fptt.append(date1).append(" 到 ").append(date3);
                dfdlDtoMerge.setFptt(fptt.toString());
                fplt.append(date2).append(" 到 ").append(date4);
                dfdlDtoMerge.setFplt(fplt.toString());
                mergeList.add(dfdlDtoMerge);
            }
        }
        return mergeList;
    }

    /**
     * 采用java8的stream流来过滤数据
     * @param list 需要过滤的集合
     * @param predicate 过滤条件
     * @return 返回过滤后的集合
     * @param <T> List的泛型
     */
    private static <T> List<T> filterList (List<T> list, Predicate<T> predicate) {
        return list.stream ()
                .filter (predicate)
                .collect (Collectors.toList ());
    }
    @Override
    public PageInfo selectA2401(String dfltFlno, String dtime, String dfltAwcd, String apcd, String fptt, String fplt, Integer pageNum, Integer pageSize) throws ParseException {
        // 将传入的日期时间字符串转换为指定格式的字符串
        String dtimeData = formatDate(dtime, true);
        String fpttData = formatDate(fptt, false);
        String fpltData = formatDate(fplt, false);
        ArrayList<DfdlStatusDto> dfdlDtoMerges = dfdlMapper.getAapno2();
        Predicate<DfdlStatusDto> predicate = dfdlDtoMerge -> true;
        if (!Objects.isNull(dfltFlno)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getDfltFlno().contains(dfltFlno));
        }
        if (!StringUtils.isEmpty(dtimeData)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getDtime().contains(dtimeData));
        }
        if (!Objects.isNull(apcd)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getApcd().contains(apcd));
        }
        if (!StringUtils.isEmpty(fpltData)&&!StringUtils.isEmpty(fpttData)) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(fpttData, df);
            LocalDateTime end = LocalDateTime.parse(fpltData, df);
            log.info("======start:{}======", start);
            log.info("=======end:{}=======", end);
            //判断LocalDateTime对象是否在指定的范围内
            predicate = predicate.and(dfdlDtoMerge ->
                    (LocalDateTime.parse(dfdlDtoMerge.getFptt(), df).isAfter(start) || LocalDateTime.parse(dfdlDtoMerge.getFptt(), df).isEqual(start)) &&
                    (LocalDateTime.parse(dfdlDtoMerge.getFplt(), df).isBefore(end) || LocalDateTime.parse(dfdlDtoMerge.getFplt(), df).isEqual(end)));
        }
        List<DfdlStatusDto> result = dfdlDtoMerges.stream()
                .filter(predicate)
                .collect(Collectors.toList());

        if (result.isEmpty()) {
            log.info("======没有数据======");
        }

        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(result, pageNum, pageSize);
        return pageInfo;
    }
    @Override
    public PageInfo selectD2401(String dfltFlno, String dtime, String dfltAwcd, String apcd, String fptt, String fplt, Integer pageNum, Integer pageSize) throws ParseException {
        // 将传入的日期时间字符串转换为指定格式的字符串
        String dtimeData = formatDate(dtime, true);
        String fpttData = formatDate(fptt, false);
        String fpltData = formatDate(fplt, false);

        ArrayList<DfdlStatusDto> dfdlDtoMerges = dfdlMapper.getDapno2();
        Predicate<DfdlStatusDto> predicate = dfdlDtoMerge -> true;
        if (!Objects.isNull(dfltFlno)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getDfltFlno().contains(dfltFlno));
        }
        if (!StringUtils.isEmpty(dtimeData)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getDtime().contains(dtimeData));
        }
        if (!Objects.isNull(apcd)) {
            predicate = predicate.and(dfdlDtoMerge -> dfdlDtoMerge.getApcd().contains(apcd));
        }
        if (!StringUtils.isEmpty(fpltData)&&!StringUtils.isEmpty(fpttData)) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(fpttData, df);
            LocalDateTime end = LocalDateTime.parse(fpltData, df);
            log.info("======start:{}======", start);
            log.info("=======end:{}=======", end);
            //判断LocalDateTime对象是否在指定的范围内
            predicate = predicate.and(dfdlDtoMerge ->
                    (LocalDateTime.parse(dfdlDtoMerge.getFptt(), df).isAfter(start) || LocalDateTime.parse(dfdlDtoMerge.getFptt(), df).isEqual(start)) &&
                            (LocalDateTime.parse(dfdlDtoMerge.getFplt(), df).isBefore(end) || LocalDateTime.parse(dfdlDtoMerge.getFplt(), df).isEqual(end)));
        }
        List<DfdlStatusDto> result = dfdlDtoMerges.stream()
                .filter(predicate)
                .collect(Collectors.toList());
        if (result.isEmpty()) {
            log.info("======没有数据======");
        }

        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(result, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 定义一个方法，接受一个日期时间字符串作为参数，返回一个格式化后的字符串
     * @param dateTime
     * @param checkParamName
     * @return
     */
    private String formatDate(String dateTime, boolean checkParamName) {
        //如果参数为空，就返回一个空字符串
        if (dateTime == null || dateTime.isEmpty()) {
            return "";
        }
        //如果参数不为空，就进行以下操作
        else {
            //解析参数为一个ZonedDateTime对象，它表示一个带有时区信息的日期时间
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
            //将ZonedDateTime对象转换为一个指定时区的ZonedDateTime对象，这里使用东八区（北京时间）
            ZonedDateTime beijingTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
            if (checkParamName) {
                //将ZonedDateTime对象格式化为一个字符串，使用指定的格式
                String formatted = beijingTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
                //返回格式化后的字符串
                return formatted;
            } else {
                //将ZonedDateTime对象格式化为一个字符串，使用指定的格式
                String formatted = beijingTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"));
                //返回格式化后的字符串
                return formatted;
            }
        }
    }

    @Override
    public PageInfo selectDApno3(String dfltFlno, String dtime, String apcd, String transitApcd, Integer pageNum, Integer pageSize) throws ParseException {

        ArrayList<TransferStatusDto> dfdlDtoMerges = dfdlMapper.getDapno3();
        String dtimeData = formatDate(dtime, true);
        Predicate<TransferStatusDto> predicate = transferMerge -> true;
        // 添加过滤条件
        if (!Objects.isNull(dfltFlno)) {
            predicate = predicate.and(transferMerge -> transferMerge.getDfltFlno().contains(dfltFlno));
        }
        //终点站
        if (!Objects.isNull(transitApcd)) {
            predicate = predicate.and(transferMerge -> transferMerge.getTransitApcd().contains(transitApcd));
        }
        //中转站
        if (!Objects.isNull(apcd)) {
            predicate = predicate.and(transferMerge -> transferMerge.getApcd().contains(apcd));
        }
        if (!Objects.isNull(dtimeData)) {
            predicate = predicate.and(transferMerge -> transferMerge.getDtime().contains(dtimeData));
        }

        // 进行过滤并收集结果
        List<TransferStatusDto> result = dfdlDtoMerges.stream()
                .filter(predicate)
                .collect(Collectors.toList());

        if (result.isEmpty()) {
            log.info("======没有数据======");
        }
        log.info("======result:{}======", result.size());
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(result, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public PageInfo selectAApno3(String dfltFlno, String dtime, String apcd, String transitApcd, Integer pageNum, Integer pageSize) throws ParseException {

        ArrayList<TransferStatusDto> dfdlDtoMerges = dfdlMapper.getAapno3();
        String dtimeData = formatDate(dtime, true);

        Predicate<TransferStatusDto> predicate = transferMerge -> true;
        // 添加过滤条件
        if (!Objects.isNull(dfltFlno)) {
            predicate = predicate.and(transferMerge -> transferMerge.getDfltFlno().contains(dfltFlno));
        }
        //中转站
        if (!Objects.isNull(transitApcd)) {
            predicate = predicate.and(transferMerge -> transferMerge.getTransitApcd().contains(transitApcd));
        }
        //起始站
        if (!Objects.isNull(apcd)) {
            predicate = predicate.and(transferMerge -> transferMerge.getApcd().contains(apcd));
        }
        if (!Objects.isNull(dtimeData)) {
            predicate = predicate.and(transferMerge -> transferMerge.getDtime().contains(dtimeData));
        }
        // 进行过滤并收集结果
        List<TransferStatusDto> result = dfdlDtoMerges.stream()
                .filter(predicate)
                .collect(Collectors.toList());

        if (result.isEmpty()) {
            log.info("======没有数据======");
        }
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(result, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public Integer insertAApno2(ArrayList<DfdlDto> a2403) throws ParseException {
//        ArrayList<DfdlDto> a2403 = dfdlMapper.getA2401();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto>> groupMap = GroupAndPageUtil.groupByAirlArptId(a2403);
        List<List<DfdlDto>> groupList = new ArrayList<>(groupMap.values());
        List<DfdlDtoMerge> dfdlDtoMerges = processA2401Dtos2(groupList, airLines, baseApueCnnm);

        for (DfdlDtoMerge dfdlDtoMerge : dfdlDtoMerges) {
            DfdlStatusDto dfdlStatusDto = new DfdlStatusDto();
            dfdlStatusDto.setStatus(0);
            BeanUtils.copyProperties(dfdlDtoMerge, dfdlStatusDto);
//            log.info("======dfdlStatusDto:{}======", dfdlStatusDto);
            dfdlMapper.insertA2Dfdl(dfdlStatusDto);
        }
        return null;
    }
    @Override
    public Integer insertDApno2(ArrayList<DfdlDto> d2401) throws ParseException {
//        ArrayList<DfdlDto> d2401 = dfdlMapper.getD2401();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto>> groupMap = GroupAndPageUtil.groupByAirlArptId(d2401);
        List<List<DfdlDto>> groupList = new ArrayList<>(groupMap.values());
        List<DfdlDtoMerge> dfdlDtoMerges = processD2401Dtos2(groupList, airLines, baseApueCnnm);

        for (DfdlDtoMerge dfdlDtoMerge : dfdlDtoMerges) {
            DfdlStatusDto dfdlStatusDto = new DfdlStatusDto();
            dfdlStatusDto.setStatus(0);
            BeanUtils.copyProperties(dfdlDtoMerge, dfdlStatusDto);
            dfdlMapper.insertD2Dfdl(dfdlStatusDto);
        }
        return null;
    }
    @Override
    public Integer insertAApno3(ArrayList<DfdlDto> a2403) throws ParseException {
//        ArrayList<DfdlDto> a2403 = dfdlMapper.getA2401();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto>> groupMap = GroupAndPageUtil.groupByAirlArptId(a2403);
        List<List<DfdlDto>> groupList = new ArrayList<>(groupMap.values());
        List<TransferMerge> transferMerges = processA2401Dtos3(groupList, airLines, baseApueCnnm);

        transferMerges.stream().filter(tm -> tm.getDtime().contains("2017年7月10日")).toList();
        log.info("======transferMerges:{}======", transferMerges.size());

        for (TransferMerge transferMerge : transferMerges) {
            TransferStatusDto transferStatusDto = new TransferStatusDto();
            transferStatusDto.setStatus(0);
            BeanUtils.copyProperties(transferMerge, transferStatusDto);
//            log.info("======transferStatusDto:{}======", transferStatusDto);
            dfdlMapper.insertA3Dfdl(transferStatusDto);
        }
        return null;
    }
    @Override
    public Integer insertDApno3(ArrayList<DfdlDto> d2401) throws ParseException {
//        ArrayList<DfdlDto> d2401 = dfdlMapper.getD2401();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto>> groupMap = GroupAndPageUtil.groupByAirlArptId(d2401);
        List<List<DfdlDto>> groupList = new ArrayList<>(groupMap.values());
        List<TransferMerge> transferMerges = processD2401Dtos3(groupList, airLines, baseApueCnnm);

        for (TransferMerge transferMerge : transferMerges) {
            TransferStatusDto transferStatusDto = new TransferStatusDto();
            transferStatusDto.setStatus(0);
            BeanUtils.copyProperties(transferMerge, transferStatusDto);
//            log.info("======transferStatusDto:{}======", transferStatusDto);
            dfdlMapper.insertD3Dfdl(transferStatusDto);
        }
        return null;
    }
}
